package deibay.client;

import java.util.logging.Level;

import deibay.DeibayLogger;
import deibay.messages.Message;
import deibay.users.User;



/**
 * @author  Joel Cordeiro
 */
public abstract class Command {

	protected Controller controller = null;
	protected Message message = null;
	
	public Command(Controller controller, Message msg)
	{
		this.message = msg;
		this.controller = controller;
	}
	
	public static Command parse(Message message, Controller controller) {
		String messageClassName = message.getClass().getSimpleName();
		String commandClassName = messageClassName.substring(0, messageClassName.length()- "Msg".length()) + "Command";
		Command cmd = null;
		
		try {
			cmd = (Command) Class.forName("deibay.client.commands." + commandClassName).getConstructors()[0].
											newInstance(controller, message);
		} catch (Exception ex) {
			DeibayLogger.getInstance().getLog().log(Level.SEVERE, "Invalid Message from: " + controller.toString() + " -> " + message.toString());			
		}
		
		return cmd;
	}
	
	public abstract void execute();
}
